home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / CDTV / cdtvtools-11 / CD-DA / Aztec / example.azt.c next >
Encoding:
C/C++ Source or Header  |  1991-01-03  |  8.6 KB  |  374 lines

  1. /***********************************************************************
  2. ***
  3. ***     CDTV Audio Examples by Carl Sassenrath (10-Dec-90)
  4. ***
  5. ***     Copyright (C) 1990 Commodore International, Ltd.
  6. ***     Permission granted to use for your CDTV programs.
  7. ***
  8. ************************************************************************
  9. ***
  10. ***     This file contains examples of how to perform a few simple
  11. ***     CD Audio commands on CDTV.  It was cranked out in a few
  12. ***     hours to help illustrate a few effects... and it all seems
  13. ***     to work (could use a little tweaking maybe). Sorry, but I have
  14. ***     not had the chance to convert it from Manx C to Lattice C, so
  15. ***     if you do, please send me a copy.  If not, I will get too it
  16. ***     RSN...  -Carl-
  17. ***
  18. ***     Send comments, problems, etc. to:
  19. ***         Sassenrath, P.O. Box 1510, Ukiah, CA 95482
  20. ***         Phone: (707) 462-4878  FAX: (707) 462-4879
  21. ***         CBM VAX: carl
  22. ***
  23. ***********************************************************************/
  24.  
  25. #include <exec/types.h>
  26. #include <exec/io.h>
  27. #include <devices/cdtv.h>
  28.  
  29. extern  struct  IOStdReq *CreateStdIO();
  30. extern  struct  MsgPort  *CreatePort();
  31. struct  IOStdReq *IOReq1 = NULL;
  32. struct  IOStdReq *IOReq2 = NULL;
  33. struct  MsgPort  *IOPort = NULL;
  34.  
  35. int     Track;
  36. struct  CDTOC   Toc[100];
  37. char    Buffer[10*2048];
  38.  
  39. char    AssertFail[] = "Assertion failed (MUST)";
  40.  
  41. #define MUST(expr)  if (!(expr)) Quit(AssertFail);
  42.  
  43.  
  44. /***********************************************************************
  45. ***
  46. ***  Main
  47. ***
  48. ***********************************************************************/
  49. main(argc,argv)
  50.     int argc;
  51.     char *argv[];
  52. {
  53.     printf("CDTV Audio Example (10-Dec-90)\n");
  54.  
  55.     Init();
  56.  
  57.     ReadTOC();      /* Read CD table of contents */
  58.  
  59.     DoIOR(IOReq1,CD_MUTE,0x7fff,1,0); /* Set to full volume */
  60.  
  61.     Play();
  62.     PlayBurst();
  63.     PlayBounce();
  64.     PlayFastFwd();
  65.     PlayAltFastFwd();
  66.     PlayShow();
  67.     PlayAbort();
  68.     PlayFade();
  69.     PlayMixedMode();
  70.  
  71.     Quit(0);
  72. }
  73.  
  74. /***********************************************************************
  75. ***
  76. ***  Init -- initialize program and structures
  77. ***
  78. ***********************************************************************/
  79. Init()
  80. {
  81.     MUST(IOPort = CreatePort(0,0));
  82.     MUST(IOReq1 = CreateStdIO(IOPort));
  83.     MUST(IOReq2 = CreateStdIO(IOPort));
  84.  
  85.     if (OpenDevice("cdtv.device",0,IOReq1,0))
  86.         Quit("CDTV Device will not open");
  87.  
  88.     *IOReq2 = *IOReq1;      /* Note: structure copy */
  89. }
  90.  
  91. /***********************************************************************
  92. ***
  93. ***  Quit -- exit program and clean-up.  Return an error in needed.
  94. ***
  95. ***********************************************************************/
  96. Quit(s)
  97.     char *s;    /* error message */
  98. {
  99.     if (IOReq1->io_Device) CloseDevice(IOReq1);
  100.     if (IOReq1)     DeleteStdIO(IOReq1);
  101.     if (IOReq2)     DeleteStdIO(IOReq2);
  102.     if (IOPort)     DeletePort(IOPort);
  103.     if (s) {printf("\nERROR: %s\n",s); exit(40);}
  104.     else exit(0);
  105. }
  106.  
  107. /***********************************************************************
  108. ***
  109. ***  DoIOR -- execute a device command
  110. ***
  111. ***********************************************************************/
  112. DoIOR(req,cmd,off,len,data)
  113.     struct IOStdReq *req;
  114.     int cmd;
  115.     long off;
  116.     long len;
  117.     APTR data;
  118. {
  119.     req->io_Command = cmd;
  120.     req->io_Offset = off;
  121.     req->io_Length = len;
  122.     req->io_Data   = data;
  123.     if (DoIO(req)) Quit("DoIO command error");
  124. }
  125.  
  126. /***********************************************************************
  127. ***
  128. ***  SendIOR -- asynchronously execute a device command
  129. ***
  130. ***********************************************************************/
  131. SendIOR(req,cmd,off,len,data)
  132.     struct IOStdReq *req;
  133.     int cmd;
  134.     long off;
  135.     long len;
  136.     APTR data;
  137. {
  138.     req->io_Command = cmd;
  139.     req->io_Offset = off;
  140.     req->io_Length = len;
  141.     req->io_Data   = data;
  142.     SendIO(req);
  143. }
  144.  
  145. /***********************************************************************
  146. ***
  147. ***  AddMSF -- add two Min:Sec:Frm values
  148. ***
  149. ***********************************************************************/
  150. CDPOS AddMSF(arg1,arg2)
  151.     CDPOS arg1, arg2;
  152. {
  153.     arg1.MSF.Frame += arg2.MSF.Frame;
  154.     if (arg1.MSF.Frame >= 75) {arg1.MSF.Frame -= 75; arg1.MSF.Second++;}
  155.  
  156.     arg1.MSF.Second += arg2.MSF.Second;
  157.     if (arg1.MSF.Second >= 60) {arg1.MSF.Second -= 60; arg1.MSF.Minute++;}
  158.  
  159.     arg1.MSF.Minute += arg2.MSF.Minute;
  160.  
  161.     return arg1;
  162. }
  163.  
  164. /***********************************************************************
  165. ***
  166. ***  ReadTOC -- read entire CD table of contents
  167. ***
  168. ***********************************************************************/
  169. ReadTOC()
  170. {
  171.     DoIOR(IOReq1,CD_TOCMSF,0,100,&Toc[0]);
  172.  
  173.     printf("CD is %02d:%02d:%02d long ",
  174.         Toc[0].Position.MSF.Minute,
  175.         Toc[0].Position.MSF.Second,
  176.         Toc[0].Position.MSF.Frame);
  177.     printf("with tracks %d to %d\n",Toc[0].Track,Toc[0].LastTrack);
  178.  
  179.     DoIOR(IOReq1,CD_ISROM,0,0,0);
  180.     printf("It %s CD-ROM data\n",
  181.         IOReq1->io_Actual ? "has" : "does not have");
  182.  
  183.     if (IOReq1->io_Actual && Toc[0].LastTrack <= 1)
  184.         Quit("No audio tracks\n");
  185.  
  186.     if (Toc[0].LastTrack <= 2) Quit("Not enough tracks\n");
  187.     Track = ((Toc[0].LastTrack >= 6) ? 6 : 2);
  188. }
  189.  
  190. /***********************************************************************
  191. ***
  192. ***  Example Functions
  193. ***
  194. ***********************************************************************/
  195. Play()
  196. {
  197.     printf("Play 20 seconds of track %d...\n",Track);
  198.     DoIOR(IOReq1,CD_PLAYMSF,Toc[Track].Position.Raw,
  199.         AddMSF(Toc[Track].Position.Raw,TOMSF(0,20,0)).Raw,0);
  200. }
  201.  
  202. PlayBurst()
  203. {
  204.     int i;
  205.     CDPOS start, stop;
  206.  
  207.     printf("Play 20 short sequential bursts...\n");
  208.     start.Raw = Toc[Track].Position.Raw;
  209.  
  210.     for (i = 0; i < 40; i++)
  211.     {
  212.         stop.Raw = AddMSF(start.Raw,TOMSF(0,0,19));
  213.         DoIOR(IOReq1,CD_PLAYMSF,start.Raw,stop.Raw,0);
  214.         start.Raw = stop.Raw;
  215.     }
  216. }
  217.  
  218. PlayBounce()
  219. {
  220.     int i;
  221.     CDPOS start, stop;
  222.  
  223.     printf("Bounce the 'needle' on the 'record'...\n");
  224.     start.Raw = AddMSF(Toc[Track].Position.Raw,TOMSF(0,6,0));
  225.     stop.Raw = AddMSF(start.Raw,TOMSF(0,0,6));
  226.  
  227.     for (i = 0; i < 30; i++)
  228.     {
  229.         DoIOR(IOReq1,CD_PLAYMSF,start.Raw,stop.Raw,0);
  230.     }
  231. }
  232.  
  233. PlayFastFwd()
  234. {
  235.     int i;
  236.     CDPOS start, stop;
  237.  
  238.     printf("Play fast forward...\n");
  239.     start.Raw = Toc[Track].Position.Raw;
  240.  
  241.     for (i = 0; i < 40; i++)
  242.     {
  243.         stop.Raw = AddMSF(start.Raw,TOMSF(0,0,19));
  244.         DoIOR(IOReq1,CD_PLAYMSF,start.Raw,stop.Raw,0);
  245.         start.Raw = AddMSF(start.Raw,TOMSF(0,1,0));
  246.     }
  247. }
  248.  
  249. PlayAltFastFwd()    /* Note: requires 2 I/O requests! */
  250. {
  251.     int i;
  252.     CDPOS start, stop;
  253.  
  254.     printf("Play alternate fast forward...\n");
  255.     start.Raw = Toc[Track].Position.Raw;
  256.     stop.Raw = AddMSF(start.Raw,TOMSF(0,40,0));
  257.     SendIOR(IOReq1,CD_PLAYMSF,start.Raw,stop.Raw,0);
  258.  
  259.     for (i = 0; i < 30; i++)
  260.     {
  261.         Delay(50/5);    /* Note: Delay() not too accurate */
  262.         start.Raw = AddMSF(start.Raw,TOMSF(0,0,30));
  263.         DoIOR(IOReq2,CD_POKEPLAYMSF,start.Raw,stop.Raw,0);
  264.     }
  265.  
  266.     AbortIO(IOReq1);
  267.     WaitIO(IOReq1);
  268. }
  269.  
  270. PlayShow()
  271. {
  272.     CDPOS start, stop;
  273.     struct CDSubQ subq;
  274.  
  275.     printf("Play and show track time...\n");
  276.     start.Raw = Toc[Track].Position.Raw;
  277.     stop.Raw = AddMSF(start.Raw,TOMSF(0,20,0));
  278.     SendIOR(IOReq1,CD_PLAYMSF,start.Raw,stop.Raw,0);
  279.  
  280.     while (!CheckIO(IOReq1))
  281.     {
  282.         DoIOR(IOReq2,CD_SUBQMSF,0,0,&subq);
  283.         if (subq.Status == SQSTAT_PLAYING)
  284.         {
  285.             printf("%02d:%02d:%02d\n",
  286.                 subq.TrackPosition.MSF.Minute,
  287.                 subq.TrackPosition.MSF.Second,
  288.                 subq.TrackPosition.MSF.Frame);
  289.         }
  290.     }
  291.  
  292.     WaitIO(IOReq1);
  293. }
  294.  
  295. PlayAbort()
  296. {
  297.     printf("Play 20 sec, abort after 10...\n");
  298.  
  299.     SendIOR(IOReq1,CD_PLAYMSF,Toc[Track].Position.Raw,
  300.         AddMSF(Toc[Track].Position.Raw,TOMSF(0,20,0)).Raw,0);
  301.  
  302.     Delay(50*10);
  303.     AbortIO(IOReq1);
  304.     WaitIO(IOReq1);
  305.     DoIOR(IOReq1,CD_STOPPLAY,0,0,0); /* only if you want to */
  306. }
  307.  
  308. PlayFade()
  309. {
  310.     CDPOS start, stop;
  311.     int i;
  312.  
  313.     printf("Play while fading up and down...\n");
  314.  
  315.     start.Raw = AddMSF(Toc[Track].Position.Raw,TOMSF(1,0,0));
  316.     stop.Raw = AddMSF(start.Raw,TOMSF(1,0,0));
  317.     SendIOR(IOReq1,CD_PLAYMSF,start.Raw,stop.Raw,0);
  318.  
  319.     DoIOR(IOReq2,CD_FADE,0x7fff,0,0);       /* full volume */
  320.  
  321.     for (i = 0; i < 5; i++)
  322.     {
  323.         DoIOR(IOReq2,CD_FADE,0,150,0);
  324.         Delay(100);
  325.         DoIOR(IOReq2,CD_FADE,0x7fff,150,0);
  326.         Delay(100);
  327.     }
  328.  
  329.     for (i = 0; i < 10; i++)
  330.     {
  331.         DoIOR(IOReq2,CD_FADE,0,15,0);
  332.         Delay(50/4);
  333.         DoIOR(IOReq2,CD_FADE,0x7fff,15,0);
  334.         Delay(50/4);
  335.     }
  336.  
  337.     DoIOR(IOReq2,CD_FADE,0,750,0);
  338.     Delay(50*10);
  339.  
  340.     AbortIO(IOReq1);
  341.     WaitIO(IOReq1);
  342.  
  343.     DoIOR(IOReq1,CD_STOPPLAY,0,0,0);    /* stop the play for good */
  344.     DoIOR(IOReq2,CD_FADE,0x7fff,0,0);       /* return to full vol */
  345. }
  346.  
  347. PlayMixedMode()
  348. {
  349.     int i;
  350.     CDPOS start, stop;
  351.  
  352.     printf("Read and Play a mixed mode disk...\n");
  353.  
  354.     DoIOR(IOReq1,CD_ISROM,0,0,0);
  355.     if (!IOReq1->io_Actual)
  356.     {
  357.         printf("Not a mixed mode disk!\n");
  358.         return;
  359.     }
  360.  
  361.     start.Raw = Toc[Track].Position.Raw;
  362.  
  363.     for (i = 0; i < 40; i++)
  364.     {
  365.         printf("Reading...\n");
  366.         DoIOR(IOReq1,CD_READ,i*10*2048,6*2048,Buffer);
  367.         if (i==0) printf("Whoops... will fix that glitch\n");
  368.         stop.Raw = AddMSF(start.Raw,TOMSF(0,1,0));
  369.         printf("Playing...\n");
  370.         DoIOR(IOReq1,CD_PLAYMSF,start.Raw,stop.Raw,0);
  371.         start.Raw = stop.Raw;
  372.     }
  373. }
  374.